# pip install paramiko -i https://pypi.tuna.tsinghua.edu.cn/simple
import paramiko


def execute_remote_command(host, username, password, command):
    # 创建SSH客户端
    client = paramiko.SSHClient()
    try:
        # 连接到远程主机
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname=host, username=username, password=password)

        # 执行命令并获取输出
        stdin, stdout, stderr = client.exec_command(command)
        output = stdout.read().decode('utf-8')
        error_output = stderr.read().decode('utf-8')

        # 打印出命令执行结果
        print("Output: ", output)
        if error_output:
            print("Error: ", error_output)
    finally:
        # 关闭合连接
        client.close()


# 示例使用
execute_remote_command('192.168.6.14', 'ps', '1', 'ls /')


import paramiko


def sftp_upload(host, username, password, local_file_path, remote_file_path):
    # 创建SSH客户端
    transport = paramiko.Transport((host, 22))
    transport.connect(username=username, password=password)

    # 创建SFTP客户端
    sftp = paramiko.SFTP.from_transport(transport)

    try:
        # 上传文件
        sftp.put(local_file_path, remote_file_path)
        print(f"文件 {local_file_path} 已上传到 {remote_file_path}")
    finally:
        # 关闭合连接
        sftp.close()
        transport.close()


# 示例使用
sftp_upload('192.168.6.14', 'ps', '1', r'D:\data\231207huoni\采图\cable\20240511\img_3_20240511_142148_415_yd_cable_NG_img.jpg', '/home/ps/zhangxiancai/data/temp/1.jpg')
